<?php

namespace app\order\logic;

use think\facade\Db;
use think\facade\Cache;
use util\Excel;
use think\exception\ValidateException;
use think\facade\Log;
use util\Token;
use util\Upload;
use think\cache\driver\Redis;
use app\api\logic\ApiOrderLogic;
use app\api\logic\ApiOrdercheckLogic;
use app\api\logic\FreightLogic;
use app\api\logic\FeiShuTalkLogic;
use app\order\logic\OrderSourceLogic;
use app\api\logic\ShopyLogic;
use app\order\logic\OrderReportSkuLogic;
use app\api\logic\FeiShuMessageLogic;
use app\data\util\HttpUtil;
use app\api\logic\CheckLogic;

class TagLabelLogic
{
    protected $model = null;

    public function __construct()
    {
        $this->model = new \app\order\model\Order();
    }

    

    /**
    * @desc    DaijinquanTag
    * @author  zhangheng
    */
    public function DaijinquanTag()
    {
        $t1       = microtime(true);
        $count = $list = Db::name('conversations')
            ->where('brand',"SheCurve")
            ->where('status',0)
            ->where('channel',1)
            ->where('tag_type',0)
            ->count();
        // halt($count);
        $pageSize = 600;
        $page     = ceil($count / $pageSize);
        $executedCount = 0;
        $executedCountsu = 0;
                
        $email        = Config('Logisticskey.apireamaze.email');
        $token        = Config('Logisticskey.apireamaze.token');

        for ($i = 0; $i <= $page; $i++){
            // $tags[$i];
            $list = Db::name('conversations')
                ->field('id,slug,brand')
                ->where('brand',"SheCurve")
                ->where('status',0)
                ->where('channel',1)
                ->where('tag_type',0)
                ->order('created_at asc')
                ->limit($pageSize)
                ->select()
                ->toArray();
            foreach ($list as $v){
                $update = [];
                $slug = $v['slug'];
                //邮箱搜索
                $url = 'https://SheCurve.reamaze.io/api/v1/conversations/'.$slug;
                $return = HttpUtil::getUrlReamaze($url, ['email' => $email, 'token' => $token]);
                $tag = $return['tag_list'] ?? [];
                if (!empty($tag)) {
                    $v['tag_list'] = json_encode($tag, true);
                }else{
                    $v['tag_list'] = "";
                }
                $status = $return['status'];
                $emails = $return['author']['email'] ?? "";
                if ($status !== 0) {
                    echo '状态是'.$status;
                    $update['status'] = $status;
                    Db::name('conversations')->where('id',$v['id'])->update($update);
                    $executedCount++;
                    $num = $count - $executedCount;
                    echo PHP_EOL . "总计:" . $count . '条' . '-----' . "已执行:" . $executedCount . '-----' . '剩余:' . $num . '条,成功修改'.$executedCountsu.'条，当前ID'.$v['id'].$v['slug'].',第' .$i.'页' . PHP_EOL;
                    continue;
                }
                //查看最新消息
                //邮箱搜索
                $url = 'https://SheCurve.reamaze.io/api/v1/conversations/'.$slug.'/messages';
                $return = HttpUtil::getUrlReamaze($url, ['email' => $email, 'token' => $token]);
                $messages = json_encode($return['messages'], true);
                $str = [
                    'voucher',
                    'coupon',
                    'gift card',
                    'partial refund',
                ];
                //是否包含
                for ($is = 0; $is < count($str); $is++){
                    if(strpos($messages, $str[$is]) !== false){
                        //修改标签
                        $return_con = $this->ModifyTagsinfo($v,"daijinquan");
                        if($return_con){
                            $update['tag_type'] = 1;
                            Db::name('conversations')->where('id',$v['id'])->update($update);
                            $executedCountsu++;
                        }
                        $executedCount++;
                        $num = $count - $executedCount;
                        echo PHP_EOL . "总计:" . $count . '条' . '-----' . "已执行:" . $executedCount . '-----' . '剩余:' . $num . '条,成功修改'.$executedCountsu.'条，当前ID'.$v['id'].$v['slug'].',第' .$i.'页' . PHP_EOL;
                        break;
                    }
                }
                $executedCount++;
                
                $update['tag_type'] = 2;
                $update['email'] = $emails;
                Db::name('conversations')->where('id',$v['id'])->update($update);

                $num = $count - $executedCount;
                echo PHP_EOL . "总计:" . $count . '条' . '-----' . "已执行:" . $executedCount . '-----' . '剩余:' . $num . '条,成功修改'.$executedCountsu.'条，当前ID'.$v['id'].$v['slug'].',第' .$i.'页' . PHP_EOL;
            }
        }
        echo PHP_EOL . "get_webhook_infos-定时执行总耗时:" . round(microtime(true) - $t1, 3) . PHP_EOL;
    }

    public function ModifyTagsinfo($v,$tags)
    {
        $brand = $v['brand'] ?? 'SheCurve';
        $tags = trim($tags);
        $slug = $v['slug'];
        $tag_list = $v['tag_list'] ?? "";
        if(empty($tag_list)){
            $tag_list = [$tags];
        }else{
            $tag_list = json_decode($tag_list,true);
            $tag_list = array_merge($tag_list,[$tags]);
        }
        $email        = Config('Logisticskey.apireamaze.email');
        $token        = Config('Logisticskey.apireamaze.token');
        //邮箱搜索
        $url = 'https://'.$brand.'.reamaze.io/api/v1/conversations/'.$slug;
        $data['conversation']['tag_list'] = $tag_list;
        $return = HttpUtil::putUrlReamaze($url,$data, ['email' => $email, 'token' => $token]);
        // halt($return);
        return true;
    }

    
    /**
    * @desc    FirstTag
    * @author  zhangheng
    */
    public function FirstTag()
    {
        $update['email'] = "";
        $update2 = Db::name('conversations')
            ->where('brand',"SheCurve")
            ->where('email',"=","support@shecurve.com")
            ->where('status',0)
            ->where('channel',1)
            ->update($update);
            echo PHP_EOL . "修改成功".$update2.PHP_EOL;

        $t1       = microtime(true);

        $count = $list = Db::name('conversations')
            ->where('brand',"SheCurve")
            ->where('email',"<>","")
            ->where('status',0)
            ->where('channel',1)
            ->where('tag_typea',0)
            ->count();
        echo PHP_EOL . "总计:" . $count . '条' . PHP_EOL;
        // halt($count);
        $pageSize = 600;
        $page     = ceil($count / $pageSize);
        $executedCount = 0;
        $executedCountsu = 0;

        for ($i = 0; $i <= $page; $i++){
            // $tags[$i];
            $list = Db::name('conversations')
                ->field('id,slug,brand,email')
                ->where('brand',"SheCurve")
                ->where('email',"<>","")
                ->where('status',0)
                ->where('channel',1)
                ->where('tag_typea',0)
                ->order('created_at asc')
                ->limit($pageSize)
                ->select()
                ->toArray();
            foreach ($list as $v){
                $customer = Db::name('order_customer')->where('email',$v['email'])->find();
                $counts = 0;
                if($customer){
                    $counts = Db::name('order_achievement')
                        ->where('shop_id',17)
                        ->where('order_id',$customer['order_id'])
                        ->where('tags','like','%Master%')
                        ->where('payment_gateway_names',"shopify_payments")
                        ->count();
                        if($counts == 0){
                            $counts = Db::name('order_achievement')
                                ->where('shop_id',17)
                                ->where('order_id',$customer['order_id'])
                                ->where('tags','like','%paypal%')
                                ->where('payment_gateway_names',"paypal")
                                ->count();
                        }
                }
                if($counts > 0){
                    $return_con = $this->ModifyTagsinfo($v,"First");
                    if($return_con){
                        $update['tag_typea'] = 1;
                        $executedCountsu++;
                    }
                }
                $executedCount++;
                
                $update['tag_typea'] = 2;
                Db::name('conversations')->where('id',$v['id'])->update($update);

                $num = $count - $executedCount;
                echo PHP_EOL . "总计:" . $count . '条' . '-----' . "已执行:" . $executedCount . '-----' . '剩余:' . $num . '条,成功修改'.$executedCountsu.'条，当前ID'.$v['id'].$v['slug'].',第' .$i.'页' . PHP_EOL;
            }
        }
        echo PHP_EOL . "get_webhook_infos-定时执行总耗时:" . round(microtime(true) - $t1, 3) . PHP_EOL;
    }

    //未发货的客户order_customer_tag
    public function UnshippedCustomers()
    {
        $t1 = microtime(true);
        
        $where = [];
        $where[]  = ['financial_status', 'in', ["partially_paid", "paid", "partially_refunded", "refunded"]];
        $where[]  = ['fulfillment_status', '<>', "fulfilled"];
        $where[]  = ['shop_id', '=', 17];
        $where[]  = ['createtime', '>=', 1741190400];
        $where[]  = ['createtime', '<=', 1743436800];
        $count = Db::name('order_achievement')
            ->where($where)
            ->count();
        echo PHP_EOL . "未发货的客户开始操作" . PHP_EOL;
        if(!empty($count)){
            $list = Db::name('order_achievement')
                ->field('order_id')
                ->where($where)
                ->select()
                ->toArray();
                $executedCount = 0;
                foreach ($list as $v) {
                    //查询客户
                    $customer = Db::name('order_customer')
                        ->where('order_id',$v['order_id'])
                        ->find();
                    if(!empty($customer)){
                        $insertData = [
                            'customer_id' => $customer['customer_id'],
                            'order_id' => $customer['order_id'],
                            'email' => $customer['email'],
                        ];
                        $order_customer_tag = Db::name('order_customer_tag')->where('order_id',$customer['order_id'])->find();
                        if(empty($order_customer_tag)){
                            Db::name('order_customer_tag')->insert($insertData);
                        }
                    }
                    $executedCount++;
                    $num = $count - $executedCount;
                    echo PHP_EOL . "未发货的客户总计:" . $count . '条' . '-----' . "已执行:" . $executedCount . '-----' . '剩余:' . $num . '条' . PHP_EOL;
                }
                $t2 = microtime(true);
                echo PHP_EOL . "未发货的客户执行时间：" . ($t2 - $t1) . '秒' . PHP_EOL;
                return true;
        }
    }

    

    /**
    * @desc    给客户打标签
    * @author  zhangheng
    */
    public function WorkOrderLabel()
    {
        $count = Db::name('order_customer_tag')
            ->where('status',0)
            ->count();
        echo "给客户打标签".$count . PHP_EOL;
        // halt($count);
        $executedCount = 0;
        $executedCountsu = 0;

            $update = [];
            $update['status'] = 2;
            $customers = Db::name('order_customer_tag')
                ->field('customer_id,email')
                ->where('status',0)
                ->order('id asc')
                ->select()->toarray();
            
            $shop_id        = 17;
            $shop_info      = Db::name('shops')->where('id',$shop_id)->find();
            foreach ($customers as $k=>$customer) {
                $where = [];
                $where[]  = ['status', '=', 0];
                $where[]  = ['channel', '=', 1];
                $where[] = ['json', 'like', '%' . $customer["email"] . '%'];
                $conversations = Db::name('conversations')
                    ->where($where)
                    ->find();
                //修改标签
                if(!empty($conversations)){
                    $this->ModifyTagsinfo($conversations,"Meifaaaaaa");
                    $executedCountsu++;
                }
                $update['status'] = 1;
                $executedCount++;
                Db::name('order_customer_tag')
                    ->where('customer_id',$customer['customer_id'])
                    ->update($update);
                echo "总共".$count."条，客户：".$customer['customer_id']."执行".$executedCount."条，成功".$executedCountsu."条" . PHP_EOL;
            }
    }


    /**
    * @desc    DisputeTag
    * @author  zhangheng
    */
    public function DisputeTag()
    {
        $t1       = microtime(true);

        //打完标签改成3没工单 4有工单
        $count = Db::name('order_achievement')
            ->where('disputeapi_type',"<>",'')
            // ->where('order_id',6983684555122)
            ->where('metafields_status',"=",1)
            ->count(); 
        echo PHP_EOL . "DisputeTag总计:" . $count . '条' . PHP_EOL;
        // halt($count);
        $pageSize = 600;
        $page     = ceil($count / $pageSize);
        $executedCount = 0;
        $executedCountsu = 0;

        for ($i = 0; $i <= $page; $i++){
            $list = Db::name('order_achievement')
                ->field('id,order_id')
                ->where('disputeapi_type',"<>",'')
                ->where('metafields_status',"=",1)
                // ->where('order_id',6983684555122)
                ->order('id desc')
                ->limit($pageSize)
                ->select()
                ->toArray();
            foreach ($list as $v){
                $update = [];
                $update['metafields_status'] = 3;

                $order_customer = Db::name('order_customer')->field('email')->where('order_id',$v['order_id'])->where('email',"<>",'')->find();
                $email = $order_customer['email'] ?? '';
                if(!empty($email)){
                    $wherecon = [];
                    $wherecon[]  = ['channel', '=', 1];
                    $wherecon[] = ['json', 'like', '%' . $email . '%'];
                    $conversations = Db::name('conversations')
                        ->where($wherecon)
                        ->find();
                    if(!empty($conversations)){
                        $update['metafields_status'] = 4;
                        $this->ModifyTagsinfo($conversations,"Dispute");
                        $executedCountsu++;
                    }
                }
                $executedCount++;
                
                Db::name('order_achievement')->where('id',$v['id'])->update($update);

                $num = $count - $executedCount;
                echo PHP_EOL . "总计:" . $count . '条' . '-----' . "已执行:" . $executedCount . '-----' . '剩余:' . $num . '条,成功修改'.$executedCountsu.'条，当前ID'.$v['order_id'].',第' .$i.'页' . PHP_EOL;
            }
        }
        echo PHP_EOL . "get_webhook_infos-定时执行总耗时:" . round(microtime(true) - $t1, 3) . PHP_EOL;
    }


    /**
    * @desc    cancellationTag
    * @author  zhangheng
    */
    public function cancellationTag()
    {
        $t1       = microtime(true);
        $count = $list = Db::name('conversations')
            ->where('brand',"SheCurve")
            ->where('status',0)
            ->where('channel',1)
            ->where('tag_typer',0)
            ->count();
        // halt($count);
        $pageSize = 600;
        $page     = ceil($count / $pageSize);
        $executedCount = 0;
        $executedCountsu = 0;
                
        $email        = Config('Logisticskey.apireamaze.email');
        $token        = Config('Logisticskey.apireamaze.token');

        for ($i = 0; $i <= $page; $i++){
            // $tags[$i];
            $list = Db::name('conversations')
                ->field('id,slug,brand')
                ->where('brand',"SheCurve")
                ->where('status',0)
                ->where('channel',1)
                ->where('tag_typer',0)
                ->order('created_at asc')
                ->limit($pageSize)
                ->select()
                ->toArray();
            foreach ($list as $v){
                $update = [];
                $slug = $v['slug'];
                //邮箱搜索
                $url = 'https://SheCurve.reamaze.io/api/v1/conversations/'.$slug;
                $return = HttpUtil::getUrlReamaze($url, ['email' => $email, 'token' => $token]);
                $tag = $return['tag_list'] ?? [];
                if (!empty($tag)) {
                    $v['tag_list'] = json_encode($tag, true);
                }else{
                    $v['tag_list'] = "";
                }
                $status = $return['status'];
                $emails = $return['author']['email'] ?? "";
                if ($status !== 0) {
                    echo '状态是'.$status;
                    $update['status'] = $status;
                    Db::name('conversations')->where('id',$v['id'])->update($update);
                    $executedCount++;
                    $num = $count - $executedCount;
                    echo PHP_EOL . "总计:" . $count . '条' . '-----' . "已执行:" . $executedCount . '-----' . '剩余:' . $num . '条,成功修改'.$executedCountsu.'条，当前ID'.$v['id'].$v['slug'].',第' .$i.'页' . PHP_EOL;
                    continue;
                }
                //查看最新消息
                //邮箱搜索
                $url = 'https://SheCurve.reamaze.io/api/v1/conversations/'.$slug.'/messages';
                $return = HttpUtil::getUrlReamaze($url, ['email' => $email, 'token' => $token]);
                $messages = json_encode($return['messages'], true);
                $str = [
                    'cancel',
                    'canceled',
                    'cancellation',
                    "don't need",
                    "don't want",
                    'money back',
                ];
                //是否包含
                $is_strpos = 0;
                for ($is = 0; $is < count($str); $is++){
                    if(strpos($messages, $str[$is]) !== false){
                        //修改标签
                        $return_con = $this->ModifyTagsinfo($v,"cancellation");
                        if($return_con){
                            $update['tag_typer'] = 1;
                            Db::name('conversations')->where('id',$v['id'])->update($update);
                            $executedCountsu++;
                            $is_strpos = 1;
                        }
                        $executedCount++;
                        $num = $count - $executedCount;
                        echo PHP_EOL . "总计:" . $count . '条' . '-----' . "已执行:" . $executedCount . '-----' . '剩余:' . $num . '条,成功修改'.$executedCountsu.'条，当前ID'.$v['id'].$v['slug'].',第' .$i.'页' . PHP_EOL;
                        break;
                    }
                }
                if($is_strpos == 0){
                    $update['tag_typer'] = 2;
                    $update['email'] = $emails;
                    Db::name('conversations')->where('id',$v['id'])->update($update);
                    $num = $count - $executedCount;
                    echo PHP_EOL . "总计:" . $count . '条' . '-----' . "已执行:" . $executedCount . '-----' . '剩余:' . $num . '条,成功修改'.$executedCountsu.'条，当前ID'.$v['id'].$v['slug'].',第' .$i.'页' . PHP_EOL;
                }
            }
        }
        echo PHP_EOL . "get_webhook_infos-定时执行总耗时:" . round(microtime(true) - $t1, 3) . PHP_EOL;
    }

    

    
    /**
    * @desc    ReturnTag
    * @author  zhangheng
    */
    public function ReturnTag()
    {
        $t1       = microtime(true);
        $count = $list = Db::name('conversations')
            ->where('brand',"SheCurve")
            ->where('status',0)
            ->where('channel',1)
            ->where('tag_typer',2)
            ->count();
        // halt($count);
        $pageSize = 600;
        $page     = ceil($count / $pageSize);
        $executedCount = 0;
        $executedCountsu = 0;
                
        $email        = Config('Logisticskey.apireamaze.email');
        $token        = Config('Logisticskey.apireamaze.token');

        for ($i = 0; $i <= $page; $i++){
            // $tags[$i];
            $list = Db::name('conversations')
                ->field('id,slug,brand')
                ->where('brand',"SheCurve")
                ->where('status',0)
                ->where('channel',1)
                ->where('tag_typer',2)
                ->order('created_at asc')
                ->limit($pageSize)
                ->select()
                ->toArray();
            foreach ($list as $v){
                $update = [];
                $slug = $v['slug'];
                //邮箱搜索
                $url = 'https://SheCurve.reamaze.io/api/v1/conversations/'.$slug;
                $return = HttpUtil::getUrlReamaze($url, ['email' => $email, 'token' => $token]);
                $tag = $return['tag_list'] ?? [];
                if (!empty($tag)) {
                    $v['tag_list'] = json_encode($tag, true);
                }else{
                    $v['tag_list'] = "";
                }
                $status = $return['status'];
                $emails = $return['author']['email'] ?? "";
                if ($status !== 0) {
                    echo '状态是'.$status;
                    $update['status'] = $status;
                    Db::name('conversations')->where('id',$v['id'])->update($update);
                    $executedCount++;
                    $num = $count - $executedCount;
                    echo PHP_EOL . "总计:" . $count . '条' . '-----' . "已执行:" . $executedCount . '-----' . '剩余:' . $num . '条,成功修改'.$executedCountsu.'条，当前ID'.$v['id'].$v['slug'].',第' .$i.'页' . PHP_EOL;
                    continue;
                }
                //查看最新消息
                //邮箱搜索
                $url = 'https://SheCurve.reamaze.io/api/v1/conversations/'.$slug.'/messages';
                $return = HttpUtil::getUrlReamaze($url, ['email' => $email, 'token' => $token]);
                $messages = json_encode($return['messages'], true);
                $str = [
                    'wrong size',
                    'wrong item',
                    'exchange',
                    'refund',
                    'return',
                    'returns',
                    'refunds',
                ];
                //是否包含
                $is_strpos = 0;
                for ($is = 0; $is < count($str); $is++){
                    if(strpos($messages, $str[$is]) !== false){
                        //修改标签
                        $return_con = $this->ModifyTagsinfo($v,"Return");
                        if($return_con){
                            $update['tag_typer'] = 3;
                            Db::name('conversations')->where('id',$v['id'])->update($update);
                            $executedCountsu++;
                            $is_strpos = 1;
                        }
                        $executedCount++;
                        $num = $count - $executedCount;
                        echo PHP_EOL . "总计:" . $count . '条' . '-----' . "已执行:" . $executedCount . '-----' . '剩余:' . $num . '条,成功修改'.$executedCountsu.'条，当前ID'.$v['id'].$v['slug'].',第' .$i.'页' . PHP_EOL;
                        break;
                    }
                }
                if($is_strpos == 0){
                    $update['tag_typer'] = 4;
                    $update['email'] = $emails;
                    Db::name('conversations')->where('id',$v['id'])->update($update);

                    $num = $count - $executedCount;
                    echo PHP_EOL . "总计:" . $count . '条' . '-----' . "已执行:" . $executedCount . '-----' . '剩余:' . $num . '条,成功修改'.$executedCountsu.'条，当前ID'.$v['id'].$v['slug'].',第' .$i.'页' . PHP_EOL;
                }
            }
        }
        echo PHP_EOL . "get_webhook_infos-定时执行总耗时:" . round(microtime(true) - $t1, 3) . PHP_EOL;
    }
    

    
    
    /**
    * @desc    LogisticTag
    * @author  zhangheng
    */
    public function LogisticTag()
    {
        $t1       = microtime(true);
        $count = $list = Db::name('conversations')
            ->where('brand',"SheCurve")
            ->where('status',0)
            ->where('channel',1)
            ->where('tag_typer',4)
            ->count();
        // halt($count);
        $pageSize = 600;
        $page     = ceil($count / $pageSize);
        $executedCount = 0;
        $executedCountsu = 0;
                
        $email        = Config('Logisticskey.apireamaze.email');
        $token        = Config('Logisticskey.apireamaze.token');

        for ($i = 0; $i <= $page; $i++){
            // $tags[$i];
            $list = Db::name('conversations')
                ->field('id,slug,brand')
                ->where('brand',"SheCurve")
                ->where('status',0)
                ->where('channel',1)
                ->where('tag_typer',4)
                ->order('created_at asc')
                ->limit($pageSize)
                ->select()
                ->toArray();
            foreach ($list as $v){
                $update = [];
                $slug = $v['slug'];
                //邮箱搜索
                $url = 'https://SheCurve.reamaze.io/api/v1/conversations/'.$slug;
                $return = HttpUtil::getUrlReamaze($url, ['email' => $email, 'token' => $token]);
                $tag = $return['tag_list'] ?? [];
                if (!empty($tag)) {
                    $v['tag_list'] = json_encode($tag, true);
                }else{
                    $v['tag_list'] = "";
                }
                $status = $return['status'];
                $emails = $return['author']['email'] ?? "";
                if ($status !== 0) {
                    echo '状态是'.$status;
                    $update['status'] = $status;
                    Db::name('conversations')->where('id',$v['id'])->update($update);
                    $executedCount++;
                    $num = $count - $executedCount;
                    echo PHP_EOL . "总计:" . $count . '条' . '-----' . "已执行:" . $executedCount . '-----' . '剩余:' . $num . '条,成功修改'.$executedCountsu.'条，当前ID'.$v['id'].$v['slug'].',第' .$i.'页' . PHP_EOL;
                    continue;
                }
                //查看最新消息
                //邮箱搜索
                $url = 'https://SheCurve.reamaze.io/api/v1/conversations/'.$slug.'/messages';
                $return = HttpUtil::getUrlReamaze($url, ['email' => $email, 'token' => $token]);
                $messages = json_encode($return['messages'], true);
                $str = [
                    'not received',
                    "haven't received",
                    "tracking",
                    "track",
                    "How long will it take to arrive",
                    "How long will it take to receive",
                    "where",
                    "when",
                    "missing",
                    "missing order",
                    "estimated delivery date",
                    "update",
                    "order status",
                    "shipped",
                    "shipping",
                    "shipment",
                    "ship",
                    "can't see the status",
                    "what is going on with my order",
                    "long to arrive",
                ];
                //是否包含
                $is_strpos = 0;
                for ($is = 0; $is < count($str); $is++){
                    if(strpos($messages, $str[$is]) !== false){
                        //修改标签
                        $return_con = $this->ModifyTagsinfo($v,"Logistic");
                        if($return_con){
                            $update['tag_typer'] = 5;
                            Db::name('conversations')->where('id',$v['id'])->update($update);
                            $executedCountsu++;
                            $is_strpos = 1;
                        }
                        $executedCount++;
                        $num = $count - $executedCount;
                        echo PHP_EOL . "总计:" . $count . '条' . '-----' . "已执行:" . $executedCount . '-----' . '剩余:' . $num . '条,成功修改'.$executedCountsu.'条，当前ID'.$v['id'].$v['slug'].',第' .$i.'页' . PHP_EOL;
                        break;
                    }
                }
                if($is_strpos == 0){
                    $update['tag_typer'] = 6;
                    $update['email'] = $emails;
                    Db::name('conversations')->where('id',$v['id'])->update($update);

                    $num = $count - $executedCount;
                    echo PHP_EOL . "总计:" . $count . '条' . '-----' . "已执行:" . $executedCount . '-----' . '剩余:' . $num . '条,成功修改'.$executedCountsu.'条，当前ID'.$v['id'].$v['slug'].',第' .$i.'页' . PHP_EOL;
                }
            }
        }
        echo PHP_EOL . "get_webhook_infos-定时执行总耗时:" . round(microtime(true) - $t1, 3) . PHP_EOL;
    }

    
    /**
    * @desc    OthersTag
    * @author  zhangheng
    */
    public function OthersTag()
    {
        $t1       = microtime(true);
        $count = $list = Db::name('conversations')
            ->where('brand',"SheCurve")
            ->where('status',0)
            ->where('channel',1)
            ->where('tag_typer',6)
            ->count();
        // halt($count);
        $pageSize = 600;
        $page     = ceil($count / $pageSize);
        $executedCount = 0;
        $executedCountsu = 0;
                
        $email        = Config('Logisticskey.apireamaze.email');
        $token        = Config('Logisticskey.apireamaze.token');

        for ($i = 0; $i <= $page; $i++){
            // $tags[$i];
            $list = Db::name('conversations')
                ->field('id,slug,brand')
                ->where('brand',"SheCurve")
                ->where('status',0)
                ->where('channel',1)
                ->where('tag_typer',6)
                ->order('created_at asc')
                ->limit($pageSize)
                ->select()
                ->toArray();
            foreach ($list as $v){
                $update = [];
                $slug = $v['slug'];
                //邮箱搜索
                $url = 'https://SheCurve.reamaze.io/api/v1/conversations/'.$slug;
                $return = HttpUtil::getUrlReamaze($url, ['email' => $email, 'token' => $token]);
                $tag = $return['tag_list'] ?? [];
                if (!empty($tag)) {
                    $v['tag_list'] = json_encode($tag, true);
                }else{
                    $v['tag_list'] = "";
                }
                $status = $return['status'];
                $emails = $return['author']['email'] ?? "";
                if ($status !== 0) {
                    echo '状态是'.$status;
                    $update['status'] = $status;
                    Db::name('conversations')->where('id',$v['id'])->update($update);
                    $executedCount++;
                    $num = $count - $executedCount;
                    echo PHP_EOL . "总计:" . $count . '条' . '-----' . "已执行:" . $executedCount . '-----' . '剩余:' . $num . '条,成功修改'.$executedCountsu.'条，当前ID'.$v['id'].$v['slug'].',第' .$i.'页' . PHP_EOL;
                    continue;
                }
                //修改标签
                $return_con = $this->ModifyTagsinfo($v,"Others");
                if($return_con){
                    $update['tag_typer'] = 7;
                    Db::name('conversations')->where('id',$v['id'])->update($update);
                    $executedCountsu++;
                }
                $executedCount++;
                $num = $count - $executedCount;
                echo PHP_EOL . "总计:" . $count . '条' . '-----' . "已执行:" . $executedCount . '-----' . '剩余:' . $num . '条,成功修改'.$executedCountsu.'条，当前ID'.$v['id'].$v['slug'].',第' .$i.'页' . PHP_EOL;
                
            }
        }
        echo PHP_EOL . "get_webhook_infos-定时执行总耗时:" . round(microtime(true) - $t1, 3) . PHP_EOL;
    }

}
